﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint.WebControls;

namespace TrafficTechFormsHistory
{
    class HelperManager
    {
        public string caml { get; set; }
        public int Allcnt { get; set; }

        public int InPrsCnt { get; set; }
        public int AppCnt { get; set; }
        public int RejCnt { get; set; }
        public string loginUser { get; set; }
        public int failedOnStartCnt { get; set; }

        DataTable dtResult;

        int cnt;

        public void CreateTable()
        {
            dtResult = CreateDataColumns("Department", "AllForms", "Inprogress", "Approved", "Rejected", "WebName","FailedToStart");
        }

        public void GetListItems(string lstName, Label lblError, string fldRefName, string WebName)
        {
            try
            {
                Allcnt = GetAllItems(lstName, WebName);

               
                
                if (lstName == "Purchase Requisition Form")
                    GetItemsWfStatus_spec("RFQAMApproved", fldRefName);
                else
                    GetItemsWfStatus("16", fldRefName);
                AppCnt = GetItems(lstName, caml, WebName);

                string b = CheckNull(AppCnt);
                if (lstName == "Purchase Requisition Form")
                    GetItemsWfStatus_spec("Rejected", fldRefName);
                else
                    GetItemsWfStatus("17", fldRefName);
                RejCnt = GetItems(lstName, caml, WebName);

                string c = CheckNull(RejCnt);
                if (lstName == "Purchase Requisition Form")
                    InPrsCnt = Allcnt - AppCnt - RejCnt;
                else
                {
                    GetItemsWfStatusInp(fldRefName);
                    InPrsCnt = GetItems(lstName, caml, WebName);
                }

                GetItemsWfStatus("6", fldRefName);
                failedOnStartCnt = GetItems(lstName, caml, WebName);

                string a = CheckNull(InPrsCnt);
                string d = CheckNull(failedOnStartCnt);

                dtResult.Rows.Add(lstName, Allcnt, a, b, c, WebName,d);

                // dtResult.Rows.Add(lstName, SubmitCnt, InPrsCnt, AppCnt, RejCnt, WebName);
            }
            catch (Exception ee)
            {
                // throw;
                lblError.Text = lstName + ":" + ee.Message;
            }
        }

        private int SubmitedItemsCnt(string lstName, string WebName)
        {

            SPQuery spq = new SPQuery();
            spq.Query = "<Where><Eq><FieldRef Name=\"Author\" /><Value Type=\"User\">" + loginUser + "</Value></Eq></Where>";
            int SubCnt = 0;
            using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb oWeb = oSite.OpenWeb(WebName))
                {
                    SubCnt = oWeb.Lists.TryGetList(lstName).GetItems(spq).Count;
                }
            }
            return SubCnt;
        }

        protected string CheckNull(int objGrid)
        {
            if (object.Equals(objGrid, 0))
            {
                return "-";
            }
            else
            {
                return objGrid.ToString();
            }
        }


        public void BindGrid(GridView gv)
        {
            gv.DataSource = dtResult;
            gv.DataBind();

        }

        void lnkBtnInp_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }
        public void GetItemsWfStatus_spec(string wfStatus, string fldRefName)
        {

            caml = "<Where><Contains><FieldRef Name='" + fldRefName + "' /><Value Type='Text'>" + wfStatus + "</Value></Contains></Where>";
        }

        public void GetItemsWfStatus(string wfStatus, string fldRefName)
        {
            //caml = "<Where><Eq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>" + wfStatus + "</Value></Eq></Where>";

            caml = "<Where><Eq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>" + wfStatus +
                "</Value></Eq></Where>";
        }

        public void GetItemsWfStatusInp(string fldRefName)
        {
            //caml = "<Where><And><Neq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>16</Value></Neq><Neq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>17</Value></Neq></And></Where>";

            caml = "<Where><And><Neq><FieldRef Name='" + fldRefName + "' /><Value Type='WorkflowStatus'>16</Value></Neq><Neq><FieldRef Name='"
                + fldRefName + "' /><Value Type='WorkflowStatus'>17</Value></Neq></And></Where>";


        }


        public int GetAllItems(string LstTitle, string WebName)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
                {
                    using (SPWeb oWeb = oSite.OpenWeb(WebName))
                    {
                        SPList oLst = oWeb.Lists.TryGetList(LstTitle);
                        SPListItemCollection oLstItmColl;
                        if (oLst != null)
                        {
                            oLstItmColl = oLst.Items;
                            cnt = oLstItmColl.Count;
                        }
                    }
                }
            });
            return cnt;
        }

        public int GetItems(string LstTitle, string camlQry, string WebName)
        {
            SPQuery qry = new SPQuery();
            qry.Query = camlQry;

            using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb oWeb = oSite.OpenWeb(WebName))
                {

                    SPList oLst = oWeb.Lists.TryGetList(LstTitle);
                    SPListItemCollection oLstItmColl = oLst.GetItems(qry);
                    cnt = oLstItmColl.Count;
                }
            }
            return cnt;
        }

        public static DataTable CreateDataColumns(params string[] colna)
        {
            DataTable dt = new DataTable();
            foreach (string item in colna)
            {
                dt.Columns.Add(item);
            }
            return dt;
        }

        public void GetLoginUser()
        {
            SPContext currentContext = SPContext.Current;
            loginUser = string.Empty;

            if (currentContext != null && currentContext.Web.CurrentUser != null)
            {
                SPWeb web = SPContext.Current.Web;
                loginUser = web.CurrentUser.Name;
            }
            else
            {
                loginUser = System.Web.HttpContext.Current.User.Identity.Name;
            }
        }



    }
}
